

var VTICommon = {
	object : function( asId ) {
		return document.getElementById( asId );
	} , 
	nvl : function( asParam ) {
		var lsReturn = "";
		if( asParam != null ) {
			lsReturn = this.trim( asParam.toString() );
		}
		if( typeof arguments[1] != "undefined" ) {
			if( lsReturn == "" ) {
				lsReturn = arguments[1];
			}
		}
		return lsReturn;
	} ,
	ltrim : function( asParam ) {
		if ( typeof arguments[1] == "undefined" ) 
			return asParam.replace ( /^\s+/g , "" );
		else
		{
			var lsParam = asParam;
			var lsParam2 = arguments[1];
			var liParam2Len = arguments[1].length;
			while( this.left( lsParam , liParam2Len ) == lsParam2 )
			{
				if ( lsParam.length < liParam2Len ) return lsParam;
				lsParam = lsParam.substring( liParam2Len );
			}
			return lsParam;
		}
	} ,	
	rtrim : function( asParam ) {
		if ( typeof arguments[1] == "undefined" ) 
			return asParam.replace ( /\s+$/g , "" );
		else
		{
			var lsParam = asParam;
			var lsParam2 = arguments[1];
			var liParam2Len = arguments[1].length;
			while( this.right( lsParam , liParam2Len ) == lsParam2 )
			{
				if ( lsParam.length < liParam2Len ) return lsParam;
				lsParam = lsParam.substring( 0 , lsParam.length - liParam2Len );
			}
			return lsParam;
		}
	} ,	
	trim : function( asParam ) {
		if ( asParam == null ) return "";
		if ( typeof arguments[1] == "undefined" ) 
			return asParam.replace ( /^\s+|\s+$/g , "" );
		else
			return this.rtrim( this.ltrim( asParam , arguments[1] ) , arguments[1] );
	} ,
	/** as_input 을 ai_len 길이만큼 as_fill을 채워서 만든다 : VtiCommon.fill( "2" , "0" , 3 ) => "002" **/
	fill : function( asParam , asFill , aiLen ) {
		if ( typeof arguments[1] == "number" ) {
			return this.fill( asParam , arguments[2] , arguments[1] );
		} else {
			var lsReturn = asParam.toString();
			while ( lsReturn.length < aiLen )
				lsReturn = asFill + lsReturn;
			return lsReturn;
		}
	} , 
	/** 왼쪽에서 특정길이로 잘라서 반환 : VtiCommon.left( "abcde" , 2 ); => "ab" **/
	left : function( asParam , aiLen ) {
		if ( aiLen < 0 ) return "";
		if ( asParam.length < aiLen ) return asParam;
		return asParam.substring( 0 , aiLen );
	} ,
	/** 오른쪽에서 특정길이로 잘라서 반환 **/
	right : function( asParam , aiLen ) {
		if ( aiLen < 0 ) return "";
		var liParamLen = asParam.length;
		if ( liParamLen < aiLen ) return asParam;
		return asParam.substring( liParamLen , liParamLen - aiLen );
	} ,
	replace : function( asParam , asStr , asReplaceStr ) {
		return asParam.split( asStr ).join( asReplaceStr );
	} ,
	/** 문자열 "0" 제거 후 숫자로 형변환 : "0000004" => 4 **/
	int : function ( asParam ) {
		var liReturn = 0;
		var liInput;
		try {
			if ( typeof asParam != "number" ) {
				liInput = parseInt( this.trim( this.ltrim( asParam , "0" ) ) );
			} else {
				liInput = asParam;
			}
			if ( !isNaN( liInput ) )	 {
				liReturn = liInput
			} else {
				if ( typeof arguments[1] == "undefined" )	{ liReturn = 0; }
				else										{ liReturn = parseInt( arguments[1] ); }
			}
		} catch( e ) { alert( e ); }
		return liReturn;
	} ,
	hashReverse : function( aoHash ) {
		var loReturn = {};
		for ( var key in aoHash ) {
			loReturn[ aoHash[key] ] = key;
		}
		return loReturn;
	}
};

var VTIFlag = {
		rowSplit : "╀" ,
		colSplit : "┯" ,
		keySplit : "┒"
};

var VTIDate = {
		
	/** 날짜 더하기 **/
	dateAdd : function( asDiv, aiAdd, aoDate ) {

		aiAdd	= new Number( aiAdd );
		var dt	= new Date( aoDate );

		switch( asDiv.toLowerCase() ) {
			
			case "yyyy": // year
				dt.setFullYear( dt.getFullYear() + aiAdd );
				break;
			case "q": // quarter
				dt.setMonth( dt.getMonth() + ( aiAdd * 3 ) );
				break;
			case "m": // month
				dt.setMonth( dt.getMonth() + aiAdd );
				break;
			case "y": // day of year
			case "d": // day
			case "w": // weekday
				dt.setDate( dt.getDate() + aiAdd );
				break;
			case "ww": // week of year
				dt.setDate( dt.getDate() + ( aiAdd * 7 ) );
				break;
			case "h": // hour
				dt.setHours( dt.getHours() + aiAdd );
				break;
			case "n": // minute
				dt.setMinutes( dt.getMinutes() + aiAdd );
				break;
			case "s": // second
				dt.setSeconds( dt.getSeconds() + aiAdd );
				break;
			case "ms": // second
				dt.setMilliseconds( dt.getMilliseconds() + aiAdd );
				break;
			default:
				return "invalid interval: '" + asDiv + "'";
		}
		return dt;
	} , 
	/** 날짜형으로 형변환 **/
	parseDate : function( asDate ) {
		if( asDate.length == 8 ) {
			return new Date(	VTICommon.int( asDate.substring( 0 , 4 ) ) , 
								VTICommon.int( asDate.substring( 4 , 6 ) ) ,
								VTICommon.int( asDate.substring( 6 ) ) );
		}
	} ,
	/** 날짜 문자열 포멧에 따라 가져오기 **/
	getDateString : function( aoDate ) {
		if ( typeof arguments[1] == "undefined" )
			return aoDate.getFullYear() + "" + VTICommon.fill( aoDate.getMonth() + 1 , '0' , 2 ) + "" + VTICommon.fill(aoDate.getDate(), '0', 2);
		else
			return aoDate.getFullYear() + arguments[1] + VTICommon.fill( aoDate.getMonth() + 1 , '0' , 2 ) + arguments[1] + VTICommon.fill(aoDate.getDate(), '0', 2);
	} , 
	getToDayString : function() {
		var loDate = new Date();
		if ( typeof arguments[1] == "undefined" )
			return loDate.getFullYear() + "" + VTICommon.fill( loDate.getMonth() + 1 , '0' , 2 ) + "" + VTICommon.fill(loDate.getDate(), '0', 2);
		else
			return loDate.getFullYear() + arguments[1] + VTICommon.fill( loDate.getMonth() + 1 , '0' , 2 ) + arguments[1] + VTICommon.fill(loDate.getDate(), '0', 2);
	} ,
	getApm : function( aiHh ) {
		if( aiHh >= 12 && aiHh < 24 )	return VTILang.PM;
		else							return VTILang.AM;
	} , 
	getApmEng : function( aiHh ) {
		return ( this.getApm( aiHh ) == VTILang.AM ) ? "AM" : "PM";
	} ,
	getApmHh : function( aiHh ) {
		var liHh = aiHh;
		if( aiHh >= 12 && aiHh <= 24 )	{
			if( aiHh > 12 ) { liHh = aiHh - 12; }
		} else if( aiHh == 0 ) {
			liHh = 12;				// 00시면 오전12시다.
		} else {
			liHh = aiHh;
		}
		return VTICommon.fill( liHh , "0" , 2 );
	} ,
	getUpdated : function() {
		var loDate = new Date();
		return "Updated " + VTICommon.fill( loDate.getMonth() + 1 , '0' , 2 ) + "." + VTICommon.fill(loDate.getDate(), '0', 2) +
				" " + this.getApm( loDate.getHours() ) + " " + this.getApmHh( loDate.getHours() + ":" + VTICommon.fill( loDate.getMinutes() , '0' , 2 )  );
	} , 
	getMinuteFromAll : function( asParam ) {
		var lsReturn = "";
		if ( asParam.indexOf( "분" ) > 0 ) {
			
		} else if ( asParam.indexOf( "시간" ) > 0 ) {
			
		} else if ( asParam.indexOf( "일" ) > 0 ) {
			
		} else {
			
		}
	} ,
	arrDayName : { "Monday":"월요일" , "Tuesday":"화요일" , "Wednesday":"수요일" , "Thursday":"목요일" , "Friday":"금요일" , "Saturday":"토요일" , "Sunday":"일요일" } ,
	arrWeekOfMonth : { "First":"첫째주" , "Second":"둘째주" , "Third":"셋째주" , "Fourth":"네째주" , "Last":"마지막주" } ,
	arrWeekName : { "January":1 , "February":2 , "March":3 , "April":4 , "May":5 , "June":6 , "July":7 , "August":8 , "September":9 , "October":10 , "November":11 , "December":12 }
};


var VTICheck = {
	findAddr : function( asParam ) {
		return ( asParam.length < 2 || ( /^\d+$/.test( asParam ) && !/^\d{3,}$/.test( asParam ) ) );
	} ,
	findAddr2 : function( asParam ) {
		return !/^[ㅏ-ㅣㄱ-ㅎ가-힣a-zA-Z0-9]+$/.test( asParam );
	}
};


/**
 * yyyymmddhhmiss
 * yymmddhhmiss
 * yyNmmN
 * @param asFormat
 * @return
 */
var VTIDateManager = function() {
	
	this.date = null;
	this.yyyy = null;
	this.mm = null;
	this.dd = null;
	this.hh = null;
	this.mi = null;
	this.ss = null;
	this.pm = false;
	this.weekday = null;
	this.dayKor = "";
	this.dayEng = "";
	this.dayCnt = 0;
	this.weekOfMonth = "";
	this.weekName = "";
	this.weekOfMonth = "";
	this.arrDayCnt = VTILang.DAYCNT;
	this.arrDayKor = VTILang.DAYNAME_ORGINAL; 
	this.arrDayEng = VTILang.DAYENG_ORGINAL;
	this.arrWeekOfMonth = VTILang.WEEKOFMONTH;
	this.arrWeekName = VTILang.WEEKNAME;
	this.hashDayName = VTILang.HASH_DAYNAME;
	this.hashWeekOfMonth = VTILang.HASH_WEEKOFMONTH;
	this.hashWeekName = VTILang.HASH_WEEKNAME;

	if( typeof arguments[0] == "undefined" ) {
		this.date = new Date();
	} else if ( typeof arguments[0] == "object" ) {
		this.date = new Date( arguments[0] );
	} else {
		this.setDate( arguments[0] ); 
	}

	this.init();
	
};
VTIDateManager.prototype.getWeekCnt = function() {
	return Math.ceil( ( this.dd + new Date( this.yyyy , this.mm - 1 , 1 ).getDay() ) / 7 );
}
VTIDateManager.prototype.init = function() {
	this.yyyy = this.date.getFullYear();
	this.mm = this.date.getMonth() + 1;
	this.dd = this.date.getDate();
	this.hh = this.date.getHours();
	this.mi = this.date.getMinutes();
	this.ss = this.date.getSeconds();
	
	if( ( this.yyyy % 4 ==0 || this.yyyy % 100 == 0 ) && ( this.yyyy % 400 == 0 ) ) {
		this.arrDayCnt[1] = 29;
    } else {
    	this.arrDayCnt[1] = 28
    }
	
	this.dayKor = this.arrDayKor[ this.date.getDay() ];
	this.dayEng = this.arrDayEng[ this.date.getDay() ];
	
	if ( this.getWeekCnt() <= 4 ) {
		this.weekOfMonth = this.arrWeekOfMonth[ this.getWeekCnt() ];
	} else {
		this.weekOfMonth = this.arrWeekOfMonth[ 5 ];
	}
	
//	alert( this.weekOfMonth );
	
};
VTIDateManager.prototype.setDate = function( asDate ) {
	var lsDate = asDate.toUpperCase();
	lsDate = VTICommon.replace( lsDate , "-" , "" );
	lsDate = VTICommon.replace( lsDate , "." , "" );
	lsDate = VTICommon.replace( lsDate , "/" , "" );
	lsDate = VTICommon.replace( lsDate , ":" , "" );
	lsDate = VTICommon.replace( lsDate , " " , "" );
	lsDate = VTICommon.replace( lsDate , "년" , "" );
	lsDate = VTICommon.replace( lsDate , "월" , "" );
	lsDate = VTICommon.replace( lsDate , "일" , "" );
	lsDate = VTICommon.replace( lsDate , "시" , "" );
	lsDate = VTICommon.replace( lsDate , "분" , "" );
	lsDate = VTICommon.replace( lsDate , "초" , "" );
	lsDate = VTICommon.replace( lsDate , "월" , "" );
	lsDate = VTICommon.replace( lsDate , "화" , "" );
	lsDate = VTICommon.replace( lsDate , "수" , "" );
	lsDate = VTICommon.replace( lsDate , "목" , "" );
	lsDate = VTICommon.replace( lsDate , "금" , "" );
	lsDate = VTICommon.replace( lsDate , "토" , "" );
	lsDate = VTICommon.replace( lsDate , "일" , "" );
	lsDate = VTICommon.replace( lsDate , "(" , "" );
	lsDate = VTICommon.replace( lsDate , ")" , "" );
	lsDate = VTICommon.replace( lsDate , "T" , "" );
	lsDate = VTICommon.replace( lsDate , "Z" , "" );
	lsDate = VTICommon.replace( lsDate , "D" , "" );
	if ( lsDate.indexOf( "오후" ) > 0 ) {
		this.pm = true;
	}
	lsDate = VTICommon.replace( lsDate , "오전" , "" );
	lsDate = VTICommon.replace( lsDate , "오후" , "" );
	if ( lsDate.indexOf( "PM" ) > 0 ) {
		this.pm = true;
	}
	lsDate = VTICommon.replace( lsDate , "AM" , "" );
	lsDate = VTICommon.replace( lsDate , "PM" , "" );
	this.yyyy = VTICommon.int( lsDate.substring( 0 , 4 ) );
	this.mm = VTICommon.int( lsDate.substring( 4 , 6 ) ) - 1;
	this.dd = VTICommon.int( lsDate.substring( 6 , 8 ) );
	if ( lsDate.length >= 10 ) {
		this.hh = VTICommon.int( lsDate.substring( 8 , 10 ) );
		if ( this.pm ) {
			this.hh += 12;
		}
	} else {
		this.hh = 0;
	}
	if ( lsDate.length >= 12 ) {
		this.mi = VTICommon.int( lsDate.substring( 10 , 12 ) );
	} else {
		this.mi = 0;
	}
	
	if ( lsDate.length >= 14 ) {
		this.ss = VTICommon.int( lsDate.substring( 12 , 14 ) );
	} else {
		this.ss = 0;
	}
	this.date = new Date( this.yyyy , this.mm , this.dd , this.hh , this.mi , this.ss );
};
VTIDateManager.prototype.getApm = function() {
	return VTIDate.getApm( this.hh );
};
VTIDateManager.prototype.getApmHh = function() {
	return VTIDate.getApmHh( this.hh );
};
VTIDateManager.prototype.getApmEng = function() {
	return VTIDate.getApmEng( this.hh );
};
VTIDateManager.prototype.getDate = function( asFormat ) {
	if( asFormat == "object" ) {
		return this.date;
	} else {
		var lsReturn = asFormat;
		lsReturn = VTICommon.replace( lsReturn , "yyyy" , this.yyyy );
		if( lsReturn.indexOf( "yy" ) > -1 )
			lsReturn = VTICommon.replace( lsReturn , "yy" , VTICommon.right( this.yyyy.toString() , 2 ) );
		if( lsReturn.indexOf( "mmN" ) > -1 )	
			lsReturn = VTICommon.replace( lsReturn , "mmN" , this.mm );
		if( lsReturn.indexOf( "mm" ) > -1 )
			lsReturn = VTICommon.replace( lsReturn , "mm" , VTICommon.fill( this.mm , "0" , 2 ) );
		if( lsReturn.indexOf( "ddN" ) > -1 )
			lsReturn = VTICommon.replace( lsReturn , "ddN" , this.dd );
		if( lsReturn.indexOf( "dd" ) > -1 )
			lsReturn = VTICommon.replace( lsReturn , "dd" , VTICommon.fill( this.dd , "0" , 2 ) );
		if( lsReturn.indexOf( "APMhh" ) > -1 )
			lsReturn = VTICommon.replace( lsReturn , "APMhh" , this.getApmHh() );
		if( lsReturn.indexOf( "hhN" ) > -1 )
			lsReturn = VTICommon.replace( lsReturn , "hhN" , this.hh );
		if( lsReturn.indexOf( "hh" ) > -1 )
			lsReturn = VTICommon.replace( lsReturn , "hh" , VTICommon.fill( VTICommon.nvl( this.hh ) , "0" , 2 ) );
		if( lsReturn.indexOf( "miN" ) > -1 )
			lsReturn = VTICommon.replace( lsReturn , "miN" , this.mi );
		if( lsReturn.indexOf( "mi" ) > -1 )
			lsReturn = VTICommon.replace( lsReturn , "mi" , VTICommon.fill( VTICommon.nvl( this.mi ) , "0" , 2 ) );
		if( lsReturn.indexOf( "ssN" ) > -1 )
			lsReturn = VTICommon.replace( lsReturn , "ssN" , this.ss );
		if( lsReturn.indexOf( "ss" ) > -1 )
			lsReturn = VTICommon.replace( lsReturn , "ss" , VTICommon.fill( VTICommon.nvl( this.ss ) , "0" , 2 ) );
		if( lsReturn.indexOf( "day" ) > -1 )
			lsReturn = VTICommon.replace( lsReturn , "day" , this.dayKor );
		if( lsReturn.indexOf( "KAMPM" ) > -1 )
			lsReturn = VTICommon.replace( lsReturn , "KAMPM" , this.getApm() );
		if( lsReturn.indexOf( "AMPM" ) > -1 )
			lsReturn = VTICommon.replace( lsReturn , "AMPM" , this.getApmEng() );
		if( lsReturn.indexOf( "KWOM" ) > -1 )
			lsReturn = VTICommon.replace( lsReturn , "KWOM" , this.hashWeekOfMonth[ this.weekOfMonth ] );
		if( lsReturn.indexOf( "KWOM" ) > -1 )
			lsReturn = VTICommon.replace( lsReturn , "WOM" , this.weekOfMonth );
		
		return lsReturn;
	}
};

var VTIMsg = {
	show : function( aoResponse ) {
		var xmlStr = aoResponse;
		var sussessParams = this.get( xmlStr );
		alert( "[" + sussessParams.result + "] " + sussessParams.resultMessage );
	} ,
	get : function( asXml ) {
		var xml = (new DOMParser()).parseFromString(asXml.replace(/^\s+/g,'').replace(/\s+$/g,''), "text/xml");
		var sResult = xml.getElementsByTagName("result")[0].childNodes[0].nodeValue;
		var sResultMessage = xml.getElementsByTagName("resultMessage")[0].childNodes[0].nodeValue;
		return { result:sResult , resultMessage:sResultMessage };
	}
};

